//
//  MainTabViewController.m
//  RentCar
//
//  Created by sunmingming on 14-3-31.
//  Copyright (c) 2014年 Ebola. All rights reserved.
//

#import "MainTabViewController.h"

#import "tooles.h"
#import "LoginViewController.h"
#import "ServiceViewController.h"
#import "UserMainViewController.h"
#import "SMMNavigationController.h"
#import "ShowMainViewController.h"

@interface MainTabViewController ()<UITabBarControllerDelegate,UIAlertViewDelegate>

@property(nonatomic,assign) int codeTime;
@property(nonatomic,strong) NSTimer *codeTimer;
@property(nonatomic,strong) UIViewController *v1;
@property(nonatomic,strong) UIViewController *v2;
@property(nonatomic,strong) UIViewController *v3;

@property(nonatomic,strong) UIBarButtonItem *leftLocButton;
@property(nonatomic,strong) UIBarButtonItem *rightLocButton;
@property(nonatomic,strong) UIBarButtonItem *rightShowButton;
@property(nonatomic,strong) UIBarButtonItem *rightLoginButton;

@end

//登录后的主界面
@implementation MainTabViewController

@synthesize v1,v2,v3;
@synthesize leftLocButton = _leftLocButton;
@synthesize rightLocButton = _rightLocButton;
@synthesize rightShowButton = _rightShowButton;
@synthesize rightLoginButton = _rightLoginButton;

//初始化
- (id)init
{
    self = [super init];//父类是RDVTabBarController是一个高可定制的 Tab Bar 控件
    if (self) {
        self.title = @"领头洋";
        self.codeTimer = nil;
        self.tabBar.tintColor = [UIColor whiteColor];
        [self.tabBar configureFlatTabBarWithColor:[UIColor black25PercentColor] selectedColor:[UIColor blackColor]];
        
        // Custom initialization
        self.v1 = [ServiceViewController new];//预定（下单流程第一步）
        self.v2 = [ShowMainViewController new];//预定（下单流程第一步）
        self.v3 = [UserMainViewController new];
        
        [self setViewControllers:@[v1,v2,v3]];//4个对象加入RDVTabBarController
        
        //地图模式切换按钮
        self.leftLocButton = [[UIBarButtonItem alloc] initWithTitle:@"北京" style:UIBarButtonItemStyleDone target:self action:@selector(changePressed:)];
        self.navigationItem.leftBarButtonItem = _leftLocButton;
        
        self.rightLocButton = [[UIBarButtonItem alloc] initWithTitle:@"注销" style:UIBarButtonItemStyleDone target:self action:@selector(logoutPressed:)];
        
        self.rightShowButton = [[UIBarButtonItem alloc] initWithTitle:@"我要晒单" style:UIBarButtonItemStyleDone target:self action:@selector(showAddPressed:)];
        
        self.rightLoginButton = [[UIBarButtonItem alloc] initWithTitle:@"登录" style:UIBarButtonItemStyleDone target:self action:@selector(showLoginPressed:)];
    }
    return self;
}

//地图与列表切换action
-(void)changePressed:(id)sender
{
//    [(ServiceViewController*)self.v1 changeArea];
    [SVProgressHUD showInfoWithStatus:@"目前领头洋仅在北京试点，不可选择其他地区"];
}

-(void)logoutPressed:(id)sender
{
    APPALL.myUserItem.username = @"";
    APPALL.myUserItem.password = @"";
    APPALL.myUserItem.token = @"";
    [APPALL.myUserItem saveToDB];
    [APPALL showLoginWindow];
}

-(void)showAddPressed:(id)sender{
    [(ShowMainViewController*)self.v2 showAdd];
}

-(void)showLoginPressed:(id)sender{
    [APPALL showLoginWindow];
}

//地图与列表切换action内方法
-(void)countDown:(id)sender
{
    self.codeTime -= 1;
    if(self.codeTime >= 0)
    {
    }
    else
    {
        [self.codeTimer invalidate];
        self.codeTimer = nil;
    }
}

//每次进入视图都调用的方法
- (void)viewDidLoad
{
    [super viewDidLoad];//父类方法
    [NSThread sleepForTimeInterval:1.5];//多线程，线程休眠1.5秒
    self.delegate = self;
    //self.delegate = self;//smm.....
}

//视图即将可见时调用。默认情况下不执行任何操作
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    //if(!APPALL.firstIntoMainTab)//no--return
    //    return;
#if 0
    APPALL.firstIntoMainTab = NO;
    int nowStatus = APPALL.myUserItem.joinStatus.intValue;
    NSLog(@"MYYYYstatus:%d",nowStatus);
    if (APPALL.myUserItem.enterView == 1) {
        self.title = @"共享租车";
        [self setSelectedIndex:0];
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_rightBarButton];
        //self.navigationItem.rightBarButtonItem = nil;
        APPALL.myUserItem.enterView = 0;
        return;
    }
    if (self.selectedIndex != 0 ) {
       self.navigationItem.rightBarButtonItem = nil;
    }else{
       self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_rightBarButton];
    }
    //self.title = @"共享租车";
    //[self setSelectedIndex:0];
    //self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_rightBarButton];
    _rightBarButton.selected = NO;
#endif
}


- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    //判断登录
    if (/*viewController == v2 || */viewController == v3)
    {
        if ([APPALL.myUserItem.username isEqualToString:@"11111111111"])
        {
            [tooles showGlobalAlertWithTitle:@"提示信息" andMsg:@"你目前处于未登录状态，此功能需要登录才能使用！"  delegate:self cancelButtonTitle:@"继续使用" otherButtonTitles:@"马上登录"];
            return NO;
        }
    }
    return YES;
}

//提示对话框（内容及跳转）
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(buttonIndex == 1)
    {
        [APPALL showLoginWindow];
    }
}

-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    self.navigationItem.leftBarButtonItem = nil;
    self.navigationItem.rightBarButtonItem = nil;
    [self.codeTimer invalidate];
    self.codeTimer = nil;
    if([APPALL.myUserItem.username isEqualToString:@"11111111111"]){
        self.navigationItem.rightBarButtonItem = _rightLoginButton;
        if(viewController == v1)
        {
            self.title = @"领头洋";
        }
        else if(viewController == v2)
        {
            self.title = @"晒单";
        }
        else if(viewController == v3)
        {
            self.title = @"账户";
        }
        return;
    }
    if(viewController == v1)
    {
        self.title = @"领头洋";
        self.navigationItem.leftBarButtonItem = _leftLocButton;
#if 0
        [self.v1 forcechangeView:self.rightBarButton];
#endif
    }
    else if(viewController == v2)
    {
        self.title = @"晒单";
        self.navigationItem.rightBarButtonItem = _rightShowButton;
    }
    else if(viewController == v3)
    {
        self.title = @"账户";
        self.navigationItem.rightBarButtonItem = _rightLocButton;
    }
}

-(void)refreshRightBar{
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

-(void)dealloc
{
    [self.codeTimer invalidate];
    self.codeTimer = nil;
}

@end